package topo.dialog;

import com.zhtelecom.common.snmp.SNMPTarget;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import topo.autosearch.ThreadNetSearch;
import topo.utils.DialogTools;

public class AutoDiscoverByNetDialog extends JDialog {
    JPanel mainPanel = new JPanel();
    JPanel topPanel = new JPanel();

    JPanel centerPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel11 = new JLabel();
    JTextField startIpTxtField = new JTextField();
    JLabel jLabel2 = new JLabel();

    JLabel jLabel3 = new JLabel();
    JRadioButton ridioBtnYes = new JRadioButton();
    JRadioButton radioBtnNo = new JRadioButton();

    JTextField readTxtField1 = new JTextField();
    JLabel jLabel5 = new JLabel();
    JLabel jLabel6 = new JLabel();
    JLabel jLabel7 = new JLabel();
    JTextField readTxtField2 = new JTextField();
    JTextField readTxtField3 = new JTextField();
    JComboBox versionCombox1 = new JComboBox();
    JComboBox versionCombox2 = new JComboBox();
    JComboBox versionCombox3 = new JComboBox();
    JTextField portTxtFiled1 = new JTextField();
    JTextField portTxtFiled2 = new JTextField();
    JTextField portTxtFiled3 = new JTextField();
    JButton okBtn = new JButton();
    JButton cancelBtn = new JButton();
    JLabel jLabel8 = new JLabel();
    JLabel jLabel9 = new JLabel();
    JLabel jLabel10 = new JLabel();

    public AutoDiscoverByNetDialog(Frame frame) {
        super(frame, "自动搜索设备", true);
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        setSize(new Dimension(450, 380));
        this.topPanel.setLayout(null);
        this.jLabel1.setText("网 络 号：");
        this.jLabel1.setBounds(new Rectangle(13, 23, 64, 27));
        this.startIpTxtField.setBounds(new Rectangle(90, 23, 180, 27));
        this.jLabel11.setForeground(Color.red);
        this.jLabel11.setRequestFocusEnabled(true);
        this.jLabel11.setHorizontalAlignment(0);
        this.jLabel11.setHorizontalTextPosition(0);
        this.jLabel11.setText("*");
        this.jLabel11.setBounds(new Rectangle(260, 25, 9, 19));

        this.jLabel2.setText("多个网络号以;分隔。例如：192.168.1.0;10.16.1.0");
        this.jLabel2.setBounds(new Rectangle(13, 44, 4000, 26));

        this.jLabel3.setText("搜索方式：");
        this.jLabel3.setBounds(new Rectangle(12, 66, 65, 24));
        this.ridioBtnYes.setText("使用Ping+SNMP");
        this.ridioBtnYes.setBounds(new Rectangle(71, 66, 110, 23));
        this.ridioBtnYes.setSelected(true);
        this.radioBtnNo.setText("仅用SNMP");
        this.radioBtnNo.setBounds(new Rectangle(184, 66, 110, 23));
        ButtonGroup bg = new ButtonGroup();
        bg.add(this.ridioBtnYes);
        bg.add(this.radioBtnNo);

        this.centerPanel.setLayout(null);

        this.jLabel5.setText("读 密 码");
        this.jLabel5.setBounds(new Rectangle(115, 18, 63, 26));
        this.jLabel6.setText("版  本");
        this.jLabel6.setBounds(new Rectangle(245, 18, 53, 26));
        this.jLabel7.setText("端口号");
        this.jLabel7.setBounds(new Rectangle(352, 18, 63, 26));
        this.readTxtField1.setText("public");
        this.readTxtField1.setBounds(new Rectangle(92, 55, 99, 26));
        this.readTxtField2.setText("public");
        this.readTxtField2.setBounds(new Rectangle(92, 99, 99, 27));
        this.readTxtField3.setBounds(new Rectangle(92, 142, 99, 28));
        this.versionCombox1.setBounds(new Rectangle(229, 55, 73, 25));
        this.versionCombox2.setBounds(new Rectangle(229, 100, 73, 26));
        this.versionCombox3.setBounds(new Rectangle(229, 143, 73, 27));

        this.versionCombox1.addItem("V1");
        this.versionCombox1.addItem("V2C");
        this.versionCombox1.setSelectedIndex(1);

        this.versionCombox2.addItem("V1");
        this.versionCombox2.addItem("V2C");
        this.versionCombox2.setSelectedIndex(0);

        this.versionCombox3.addItem("V1");
        this.versionCombox3.addItem("V2C");
        this.versionCombox3.setSelectedIndex(1);

        this.portTxtFiled1.setBounds(new Rectangle(345, 55, 51, 24));
        this.portTxtFiled2.setBounds(new Rectangle(345, 100, 51, 25));
        this.portTxtFiled3.setBounds(new Rectangle(345, 145, 51, 24));
        this.portTxtFiled1.setText("161");
        this.portTxtFiled2.setText("161");
        this.portTxtFiled3.setText("161");

        this.okBtn.setBounds(new Rectangle(101, 6, 80, 34));
        this.okBtn.setText("确定");
        this.okBtn.addActionListener(new AutoDiscoverByNetDialog_okBtn_actionAdapter(this));

        this.buttonPanel.setLayout(null);
        this.cancelBtn.setBounds(new Rectangle(241, 6, 80, 34));
        this.cancelBtn.setText("取消");
        this.cancelBtn.addActionListener(new AutoDiscoverByNetDialog_cancelBtn_actionAdapter(this));

        this.jLabel8.setText("第一组：");
        this.jLabel8.setBounds(new Rectangle(13, 55, 65, 25));
        this.jLabel9.setText("第二组：");
        this.jLabel9.setBounds(new Rectangle(13, 99, 60, 26));
        this.jLabel10.setText("第三组：");
        this.jLabel10.setBounds(new Rectangle(13, 142, 59, 27));
        getContentPane().add(this.mainPanel, "Center");
        this.topPanel.setBorder(new TitledBorder("基本参数"));
        this.centerPanel.setBorder(new TitledBorder("SNMP参数"));
        this.centerPanel.setBounds(new Rectangle(5, 112, 429, 186));
        this.buttonPanel.setBounds(new Rectangle(5, 298, 429, 44));
        this.mainPanel.add(this.topPanel);
        this.topPanel.add(this.jLabel1);
        this.topPanel.add(this.startIpTxtField);
        this.topPanel.add(this.jLabel11);
        this.topPanel.add(this.jLabel2);
        this.topPanel.add(this.jLabel3);
        this.topPanel.add(this.ridioBtnYes);
        this.topPanel.add(this.radioBtnNo);
        this.centerPanel.add(this.jLabel7);
        this.centerPanel.add(this.jLabel5);
        this.centerPanel.add(this.readTxtField1);
        this.centerPanel.add(this.jLabel6);
        this.centerPanel.add(this.versionCombox1);
        this.centerPanel.add(this.portTxtFiled1);
        this.centerPanel.add(this.readTxtField2);
        this.centerPanel.add(this.versionCombox2);
        this.centerPanel.add(this.versionCombox3);
        this.centerPanel.add(this.readTxtField3);
        this.centerPanel.add(this.portTxtFiled2);
        this.centerPanel.add(this.portTxtFiled3);
        this.centerPanel.add(this.jLabel8);
        this.centerPanel.add(this.jLabel9);
        this.centerPanel.add(this.jLabel10);
        this.mainPanel.add(this.buttonPanel);
        this.buttonPanel.add(this.okBtn);
        this.buttonPanel.add(this.cancelBtn);
        this.mainPanel.add(this.centerPanel);
        this.topPanel.setBounds(new Rectangle(5, 4, 429, 103));
        this.mainPanel.setLayout(null);
    }

    public void okBtn_actionPerformed(ActionEvent e) {
        List snmpTargetList = new ArrayList();
        List netList = new ArrayList();
        boolean isUsePing = false;
        String startIp = this.startIpTxtField.getText().trim();

        if ((startIp == null) || (startIp.equals(""))) {
            DialogTools.showErroMsg(this, "请输入网络号地址!");
            return;
        }

        if ((this.readTxtField1.getText() == null) || (this.readTxtField1.getText().equals(""))) {
            DialogTools.showErroMsg(this, "请输入第一组SNMP参数!");
            return;
        }

        if (this.radioBtnNo.isSelected())
            isUsePing = false;
        else if (this.ridioBtnYes.isSelected()) {
            isUsePing = true;
        }
        String[] nets = startIp.split(";");
        for (int i = 0; i < nets.length; i++) {
            netList.add(nets[i]);
        }
        String read1 = this.readTxtField1.getText().trim();
        String read2 = this.readTxtField2.getText().trim();
        String read3 = this.readTxtField3.getText().trim();

        String port1 = this.portTxtFiled1.getText().trim();
        String port2 = this.portTxtFiled2.getText().trim();
        String port3 = this.portTxtFiled3.getText().trim();

        SNMPTarget target1 = new SNMPTarget();
        target1.readCommunity = read1;
        target1.port = new Integer(port1).intValue();
        if (this.versionCombox1.getSelectedIndex() == 0)
            target1.snmpVersion = 0;
        else if (this.versionCombox1.getSelectedIndex() == 1)
            target1.snmpVersion = 1;
        snmpTargetList.add(target1);

        if ((read2 != null) && (!read2.equals(""))) {
            SNMPTarget target2 = new SNMPTarget();
            target2.readCommunity = read2;
            if ((port2 != null) && (!port2.equals("")))
                target2.port = new Integer(port2).intValue();
            else
                target2.port = 161;
            if (this.versionCombox2.getSelectedIndex() == 0)
                target2.snmpVersion = 0;
            else if (this.versionCombox2.getSelectedIndex() == 0)
                target2.snmpVersion = 1;
            snmpTargetList.add(target2);
        }

        if ((read3 != null) && (!read3.equals(""))) {
            SNMPTarget target3 = new SNMPTarget();
            target3.readCommunity = read3;
            if ((port3 != null) && (!port3.equals(""))) {
                target3.port = new Integer(port3).intValue();
            } else
                target3.port = 161;
            if (this.versionCombox3.getSelectedIndex() == 0)
                target3.snmpVersion = 0;
            else if (this.versionCombox3.getSelectedIndex() == 0) {
                target3.snmpVersion = 1;
            }
            snmpTargetList.add(target3);
        }

        try {
            new ThreadNetSearch(netList, snmpTargetList, isUsePing).start();
            DialogTools.showOkMsg(this, "命令下发成功", "自动搜索已启动，请稍后...");
        } catch (Exception e1) {
            DialogTools.showErroMsg(this, "自动搜索出错!");
            e1.printStackTrace();
        }
        dispose();
    }

    public void cancelBtn_actionPerformed(ActionEvent e) {
        dispose();
    }
}